package models

import "gorm.io/gorm"

type Cart struct {
	gorm.Model
	UserId  int64
	GoodsId int64
	Title   string
	Price   float64
	Num     int64
}

func (c *Cart) AddCart() error {
	return DB.Create(&c).Error
}

func (c *Cart) GetGoodsByGoodsId(goodsId int64) error {
	return DB.Where("goods_id=?", goodsId).Limit(1).Find(&c).Error
}

func (c *Cart) GetUserCartGoodsByGoodsId(userId, goodsId int64) error {
	return DB.Where("user_id=? and goods_id=?", userId, goodsId).Limit(1).Find(&c).Error
}

func (c *Cart) NumIncr(userId, goodsId, num int64) error {
	return DB.Model(&c).Where("user_id=? and goods_id=?", userId, goodsId).Update("num", num).Error
}

func (c *Cart) DelCart(userId, goodsId int64) error {

	//if goodsId != 0 {
	return DB.Where("user_id=? and goods_id=?", userId, goodsId).Delete(&c).Error
	//}

	//return DB.Where("user_id=?", userId).Delete(&c).Error
}
